package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.fragment.AllLotteryFragment;
import com.lottery.ruok.lottrey.retrofitModule.response.GetWholeCategoryResponse;

import java.util.List;

import glideHelp.GlideHelper;

/**
 * Created by ASUS on 2017/11/13.
 */

public class AlllotteryGridAdapter extends BaseAdapter {
    private List<GetWholeCategoryResponse.ItemsBean> itemsBeen;
    private Context mContext;
    private AllLotteryFragment allLotteryFragment;

    public AlllotteryGridAdapter(List<GetWholeCategoryResponse.ItemsBean> itemsBeen, Context context, AllLotteryFragment allLotteryFragment) {
        this.itemsBeen = itemsBeen;
        mContext = context;
        this.allLotteryFragment = allLotteryFragment;
    }

    @Override
    public int getCount() {
        return  itemsBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return  itemsBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from
                    (mContext).inflate(R.layout.all_category_item, null, false);
            holder.logo = convertView.findViewById(R.id.logo);
            holder.name = convertView.findViewById(R.id.name);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(itemsBeen.get(position).getName());

        GlideHelper.loadImage(mContext, itemsBeen.get(position).getImage_url(), holder.logo);

        convertView.setOnClickListener(null);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                allLotteryFragment.gotoLotteryDetailActivity(itemsBeen.get(position).getCode(), itemsBeen.get(position).getItem_class());
            }
        });

        return convertView;

    }

    static class ViewHolder {
        ImageView logo;
        TextView name;
    }
}
